//
//  XYDownloadTableViewCell.m
//  XYSfxyxuSwift
//
//  Created by 许洋 on 2018/4/19.
//  Copyright © 2018年 许洋. All rights reserved.
//

#import "XYDownloadTableViewCell.h"
@interface XYDownloadTableViewCell()
@property (weak, nonatomic) IBOutlet UILabel *downloadName;
@property (weak, nonatomic) IBOutlet UILabel *downloadState;
@property (weak, nonatomic) IBOutlet UILabel *downloadSize;
@property (weak, nonatomic) IBOutlet UIProgressView *downloadProgress;

@end
@implementation XYDownloadTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
- (void)bindingModel:(XYDownloadModel *)model{
    _downloadName.text  = [model.downloadUrl lastPathComponent];
    
}
- (void)setFileInfo:(ZFFileModel *)fileInfo {
    _fileInfo = fileInfo;
    self.downloadName.text = fileInfo.fileName;
    // 服务器可能响应的慢，拿不到视频总长度 && 不是下载状态
    if ([fileInfo.fileSize longLongValue] == 0 && !(fileInfo.downloadState == ZFDownloading)) {
        self.downloadSize.text = @"";
        if (fileInfo.downloadState == ZFStopDownload) {
            self.downloadState.text = @"已暂停";
        } else if (fileInfo.downloadState == ZFWillDownload) {
//            self.downloadBtn.selected = YES;
            self.downloadState.text = @"等待下载";
        }
        self.downloadProgress.progress = 0.0;
        return;
    }
    NSString *currentSize = [ZFCommonHelper getFileSizeString:fileInfo.fileReceivedSize];
    NSString *totalSize = [ZFCommonHelper getFileSizeString:fileInfo.fileSize];
    // 下载进度
    float progress = (float)[fileInfo.fileReceivedSize longLongValue] / [fileInfo.fileSize longLongValue];
    
    self.downloadState.text = [NSString stringWithFormat:@"%@ / %@ (%.2f%%)",currentSize, totalSize, progress*100];
    
    self.downloadProgress.progress = progress;
    
    // NSString *spped = [NSString stringWithFormat:@"%@/S",[ZFCommonHelper getFileSizeString:[NSString stringWithFormat:@"%lu",[ASIHTTPRequest averageBandwidthUsedPerSecond]]]];
//    if (fileInfo.speed) {
//        NSString *speed = [NSString stringWithFormat:@"%@ 剩余%@",fileInfo.speed,fileInfo.remainingTime];
//        self.downloadState.text = speed;
//    } else {
//        self.downloadState.text = @"正在获取";
//    }
    
    if (fileInfo.downloadState == ZFDownloading) { //文件正在下载
//        self.downloadBtn.selected = NO;
    } else if (fileInfo.downloadState == ZFStopDownload&&!fileInfo.error) {
//        self.downloadBtn.selected = YES;
        self.downloadState.text = @"已暂停";
    }else if (fileInfo.downloadState == ZFWillDownload&&!fileInfo.error) {
//        self.downloadBtn.selected = YES;
        self.downloadState.text = @"等待下载";
    } else if (fileInfo.error) {
//        self.downloadBtn.selected = YES;
        self.downloadState.text = @"错误";
    }
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
